package com.EJ.ejcompanyweb.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.EJ.ejcompanyweb.R;
import com.EJ.ejcompanyweb.WebActivity;
import com.EJ.ejcompanyweb.entity.DateEntity;
import com.EJ.ejcompanyweb.entity.ItemEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private ArrayList<ItemEntity> mList;
    private String type;
    private ArrayList<DateEntity> DateEntityS=new ArrayList<DateEntity>() ;
    public MyAdapter(ArrayList<ItemEntity> mList, String type) {
        this.mList = mList;
        this.type = type;
    }
    public MyAdapter(ArrayList<DateEntity> DateEntityS) {
      this.DateEntityS=DateEntityS;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

      //  View view=  View.inflate(parent.getContext(),R.layout.adapter_list,null);
       // ViewHolder viewHolder=new ViewHolder(view);
        //或者
        ViewHolder viewHolder  =new  ViewHolder(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_list,parent,false)
        );
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bind(DateEntityS.get(position));
    }

    @Override
    public int getItemCount() {
        return DateEntityS.size();
    }

  public   class ViewHolder extends RecyclerView.ViewHolder {
      public ViewHolder(@NonNull View itemView) {
          super(itemView);
      }
      public void bind(final DateEntity dateEntity){

          TextView ad_title=itemView.findViewById(R.id.adapter_title);
          TextView ad_des=itemView.findViewById(R.id.adapter_des);
          TextView ad_time_and_class=itemView.findViewById(R.id.adapter_time_and_class);
          ImageView  ad_like=itemView.findViewById(R.id.adapter_like);
          ImageView  ad_img=itemView.findViewById(R.id.adapter_img);
          //商品名称
          ad_title.setText(dateEntity.getTitles());
          //商品连接
          ad_des.setText(dateEntity.getAlt());
          //价格
          ad_time_and_class.setText(dateEntity.getPrice());

          if (dateEntity.getUrl()!=null){
             Picasso.with(itemView.getContext()).load(dateEntity.getBitmapUrl()).into(ad_img);
          }else {

          }


          itemView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent=new Intent(itemView.getContext(), WebActivity.class);
                  intent.putExtra("url",dateEntity.getUrl());
                  intent.putExtra("title",dateEntity.getTitles());
                 // intent.putExtra("is_like",dateEntity.isIs_like());
                 // intent.putExtra("type",type);
                  intent.putExtra("DateEntity", (DateEntity) dateEntity);
                  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  itemView.getContext().startActivity(intent);
              }
          });
      }
  }
}
